package com.tz.cartelo.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.tz.cartelo.BaseApplication;
import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.util.EventBusSendMessage;
import com.tz.common.util.LogUtil;

import org.simple.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends DataLoadActivity implements View.OnClickListener {
    //每个文件中定义Log输出的Tag常量
    private static final String TAG = "MainActivity";
    private static final int EXIT_TIME = 2000;
    private static final int FRAGMENT_COUNT = 2;
    private static final int POSITION_GOODS_CENTER = 0;
    private static final int POSITION_MANAGER_CENTER = 1;
    private Fragment[] mFragments;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RadioGroup mMainRadioGroup;
    private static Boolean isExit = false;
    private int mMyCheckPosition;
    private PopupWindow popupWindow;
    private Api mApi;
    private TextView main_goods_center_text, main_manager_center_text;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        BaseApplication.addActivity(this);
        EventBus.getDefault().register(this);

        mFragments = new Fragment[FRAGMENT_COUNT];
        fragmentManager = getSupportFragmentManager();
        mFragments[POSITION_GOODS_CENTER] = fragmentManager.findFragmentById(R.id.main_goods_center_fragment);
        mFragments[POSITION_MANAGER_CENTER] = fragmentManager.findFragmentById(R.id.main_manage_center_fragment);
        fragmentTransaction = fragmentManager.beginTransaction()
                .hide(mFragments[POSITION_GOODS_CENTER])
                .hide(mFragments[POSITION_MANAGER_CENTER]);
        fragmentTransaction.show(mFragments[POSITION_GOODS_CENTER]).commit();

        mMainRadioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
        mMainRadioGroup.check(R.id.main_goods_center_radio);
        findViewById(R.id.main_goods_center_radio).setOnClickListener(this);
        findViewById(R.id.main_manage_center_radio).setOnClickListener(this);

        main_goods_center_text = (TextView) findViewById(R.id.main_goods_center_text);
        main_goods_center_text.setOnClickListener(this);
        main_manager_center_text = (TextView) findViewById(R.id.main_manager_center_text);
        main_manager_center_text.setOnClickListener(this);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case GET_SHARE_URL:
//                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                break;
            case LOGIN_LOG:
//                params.addParam("login", PreferencesUtils.getString(Key.PER_USER_LOGIN_NUM, ""));
//                params.addParam("type", "android");
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
     /*   switch (api) {
            case GET_SHARE_URL:
                ResultShareUrl resultShareUrl = fromJson(response, ResultShareUrl.class);
                if (!resultShareUrl.isSuccess()) {
                    showToast(resultShareUrl.getMessage());
                    return;
                }
                if (CommonUtils.isWeixinAvilible(this)) {
                    sharePop(resultShareUrl.getShareUrl().getUrl());
                } else {
                    showToast(R.string.we_chat_not_available);
                }
                break;
            case LOGIN_LOG:
                Result result = fromJson(response, Result.class);
                if (!result.isSuccess()) {
                    showToast(result.getMessage());
                    return;
                }
                break;
            default:
                break;
        }*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_goods_center_radio:
            case R.id.main_goods_center_text:
//                if (PreferencesUtils.getBoolean(Key.PER_LOGIN, false)) {
                mMainRadioGroup.check(R.id.main_goods_center_radio);
                fragmentSelect(POSITION_GOODS_CENTER);
//                } else {
//                    mMyCheckPosition = POSITION_GOODS_CENTER;
//                    switchActivityForResult(LoginActivity.class, MANAGER_CENTER, null);
//                }
                break;
            case R.id.main_manage_center_radio:
            case R.id.main_manager_center_text:
//                if (PreferencesUtils.getBoolean(Key.PER_LOGIN, false)) {
                mMainRadioGroup.check(R.id.main_manage_center_radio);
                fragmentSelect(POSITION_MANAGER_CENTER);
//                } else {
//                    mMyCheckPosition = POSITION_MANAGER_CENTER;
//                    switchActivityForResult(LoginActivity.class, MANAGER_CENTER, null);
//                }
                break;
            default:
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
      /*      switch (requestCode) {
                case MANAGER_CENTER:
                    fragmentSelect(mMyCheckPosition);
                    break;
                case WE_CHAT_SHARE:
                    mApi = Api.GET_SHARE_URL;
                    loadData(mApi, false);
                    break;
                default:
                    break;
            }*/
        }
    }

    private void fragmentSelect(int position) {
        fragmentTransaction = fragmentManager.beginTransaction()
                .hide(mFragments[POSITION_GOODS_CENTER])
                .hide(mFragments[POSITION_MANAGER_CENTER]);
        switch (position) {
            case POSITION_GOODS_CENTER:
                main_goods_center_text.setTextColor(getResources().getColor(R.color.white));
                main_manager_center_text.setTextColor(getResources().getColor(R.color.gray_text_color));
                fragmentTransaction.show(mFragments[POSITION_GOODS_CENTER]).commit();
                break;
            case POSITION_MANAGER_CENTER:
                main_goods_center_text.setTextColor(getResources().getColor(R.color.gray_text_color));
                main_manager_center_text.setTextColor(getResources().getColor(R.color.white));
                fragmentTransaction.show(mFragments[POSITION_MANAGER_CENTER]).commit();
                EventBus.getDefault().post(new EventBusSendMessage(Key.GET_MESSAGE));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        exitBy2Click();
    }

    private void exitBy2Click() {
        Timer tExit;
        if (!isExit) {
            isExit = true; // 准备退出
            showToast(R.string.exit);
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, EXIT_TIME); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }
}
